using System;
using System.Data.Services.Client;
using System.Linq;

namespace RxHeat.OData
{
    /// <summary>
    ///     RxHeat OData Observable helper extension methods
    /// </summary>
    public static class ObservableExtensions
    {
        /// <summary>
        ///     Executes the <see cref="IQueryable{T}" /> on the associated
        ///     <see cref="DataServiceContext" /> and uses a <see cref="ODataObservableResults{TEntry}" />
        ///     to push results through an <see cref="IObservable{T}" />
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="dbContext">The db context.</param>
        /// <param name="query">The query.</param>
        /// <returns></returns>
        public static IObservable<TEntity> ToObservable<TEntity>(this DataServiceContext dbContext,
                                                                 IQueryable<TEntity> query)
        {
            return new ODataObservableResults<TEntity>(query, dbContext);
        }
    }
}